package me.lc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import android.content.ContentValues;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

/**
 * 设置短信铃声Helper
 * 
 * @author davelin
 * 
 */
public class SetNotificationHelper {

	static DecimalFormat format = new DecimalFormat("00");

	public static void setNotification(int soundIndex) {
		boolean result = false;
		InputStream inputStream = null;
		String basePath = getSDPath();
		String path = null;
		if (basePath != null) {

			try {
				String fileName = "/" + "p" + format.format(soundIndex + 1)
						+ ".mp3";
				int pId = FunPeaceActivity.Instance.getResources()
						.getIdentifier("p" + format.format(soundIndex + 1),
								"raw",
								FunPeaceActivity.Instance.getPackageName());
				inputStream = FunPeaceActivity.Instance.getResources()
						.openRawResource(pId);
				byte[] reader = new byte[inputStream.available()];
				while (inputStream.read(reader) != -1) {
				}
				String dirPath = basePath + "/funpeace";
				path = dirPath + fileName;
				result = writefile(reader, dirPath, path);
			} catch (IOException e) {
				e.printStackTrace();
				displayToast("设置失败");
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
					}
				}
			}
			if (result) {
				setMyRingtone(path);
				displayToast("设置短信铃声成功!");
			} else {
				displayToast("设置失败");
			}
		} else {
			displayToast("设置失败，没有找到sd卡");
		}
	}

	private static boolean writefile(byte[] str, String dirPath, String filePath) {
		File file;
		File dir;
		FileOutputStream out;
		try {
			// 创建文件
			dir = new File(dirPath);
			file = new File(filePath);

			if (!dir.exists()) {
				dir.mkdirs();
			}

			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();

			// 打开文件file的OutputStream
			out = new FileOutputStream(file);

			// 将字符串转换成byte数组写入文件
			out.write(str);
			// 关闭文件file的OutputStream
			out.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	private static void setMyRingtone(String path) {
		File k = new File(path);
		ContentValues values = new ContentValues();
		values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
		values.put(MediaStore.MediaColumns.TITLE, k.getName());
		values.put(MediaStore.MediaColumns.SIZE, 8474325);
		values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
		values.put(MediaStore.Audio.Media.ARTIST, "Madonna");
		values.put(MediaStore.Audio.Media.DURATION, 230);
		values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
		values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
		values.put(MediaStore.Audio.Media.IS_ALARM, false);
		values.put(MediaStore.Audio.Media.IS_MUSIC, false);
		// Insert it into the database
		Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
				.getAbsolutePath());
		Uri newUri = FunPeaceActivity.Instance.getContentResolver().insert(uri,
				values);
		RingtoneManager.setActualDefaultRingtoneUri(FunPeaceActivity.Instance,
				RingtoneManager.TYPE_NOTIFICATION, newUri);
	}

	private static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
			return sdDir.toString();
		} else {
			return null;
		}

	}

	public static void displayToast(String str) {
		Toast.makeText(FunPeaceActivity.Instance, str, Toast.LENGTH_SHORT)
				.show();
	}
}
